package com.katsu.myapplib.http.compress;

import android.graphics.Bitmap;

import androidx.annotation.NonNull;

import com.katsu.myapplib.app.AppApplication;
import com.katsu.myapplib.util.UtilBitmap;

import java.io.File;

public class CompressFile extends File {

    private int w = 720, h = 1280, s = 2 * 1024 * 1024;

    public CompressFile(@NonNull String pathName, int size) {

        this(pathName, -1, -1, size);

    }

    public CompressFile(@NonNull String pathName, int width, int height) {

        this(pathName, width, height, -1);

    }

    public CompressFile(@NonNull String pathName, int width, int height, int size) {

        super(pathName);

        w = width;

        h = height;

        s = size;

    }

    public File c() {

        File f;

        try {

            Bitmap b = null;

            if (w == -1 && h == -1 && s == -1) {

                b = UtilBitmap.compressZoomQuality(getPath(), w, h, s);

            } else if (w == -1 && h == -1) {

                b = UtilBitmap.compressZoom(getPath(), w, h);

            } else if (s == -1) {

                b = UtilBitmap.compressQuality(getPath(), s);

            }

            f = UtilBitmap.saveBitmap(b, AppApplication.INSTANCE.getCameraCropCacheFodler() + System.currentTimeMillis() + ".jpg");

            b.recycle();

            if (f == null) {

                f = this;

            }

        } catch (Exception e) {

            f = this;

        }

        return f;

    }
}
